
$(document).ready(function(){
	var Main = {
			created(){
				this.getActivityBaomingxiangmuByXueshengId();
			},
			filters:{
				formateDate(time){
					return new Date(time).Format("yyyy-MM-dd");
				}
			},
		    data () {
					return {
						ctx:ctx,
						studentId:studentId,
						bms:[],
						pageNum:1,
						pageCount:0
					}
			},
			methods:{
				getActivityBaomingxiangmuByXueshengId(){
					$(".loading").show();
					let that = this;
					$.ajax({
						type: "POST",
						url: ctx + "/getActivityBaomingxiangmuByXueshengId.do",
						data: {pageIndex:that.pageNum,pageSize:10,studentId:that.studentId},
						success: function(data) {
							$(".loading").hide();
							if(data.state){
								that.pageCount = data.pageCount;
								that.showPage(that.pageNum,that.pageCount);
								that.bms = data.activityBaomingXiangmus;
							}else{
								dialog({
						  			type:'error',
						  			title:'提示',
						  			content:data.msg
								});
							}
						}
					});
				},
				//修改报名信息
				updateBm(index){
					let that = this;
					let temp = this.bms[index];
					var d=new Date(); 
					d.setDate(d.getDate()-1); 
					if(temp.jzbmsj<=d.getTime()){
						dialog({
				  			type:'error',
				  			title:'提示',
				  			content:"该活动的报名时间已截止，不能再修改"
						});
						return;
					}
					if(temp.tjzt=="1"){
						dialog({
				  			type:'warn',
				  			title:'提示',
				  			content:"该活动已提交，不能再修改"
						});
						return;
					}
					window.location.href="/kpg/signUp.do?activityId="+temp.hd_id+"&bmId="+temp.id;
				},
				changeTjzt(index,tjzt){
					let that = this;
					let bmxm = this.bms[index];
					var d=new Date(); 
					d.setDate(d.getDate()-1); 
					if(bmxm.jzbmsj<d.getTime()){
						dialog({
				  			type:'error',
				  			title:'提示',
				  			content:"该活动的报名时间已截止，不能在操作"
						});
						return;
					}
					dialog({
			  			type:'warn',
			  			title:'提示',
			  			content:(tjzt=="1"?"确认提交报名信息吗？":"确认撤回报名信息吗？"),
			  			confirm:function(){
			  				$(".loading").show();
			  				$.ajax({
								type: "POST",
								url: ctx + "/changeTjzt.do",
								data: {id:bmxm.id,tjzt:tjzt},
								success: function(data) {
									console.log(data);
									if(data.state){
										$(".loading").hide();
										dialog({
								  			type:'info',
								  			title:'提示',
								  			content:"操作成功"
										});
										that.bms[index].tjzt=tjzt;
									}
								}
							});
				  		}
					});
				},
				beginTest(id){
					let that = this;
					let sUserAgent = navigator.userAgent.toLowerCase();
		            let bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
		            let bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
		            let bIsMidp = sUserAgent.match(/midp/i) == "midp";
		            let bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
		            let bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
		            let bIsAndroid = sUserAgent.match(/android/i) == "android";
		            let bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
		            let bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
		            if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
		            	dialog({
				  			type:'info',
				  			title:'提示',
				  			content:"系统检测到你在移动端访问系统，请在电脑端进行答题。并且请使用谷歌或360浏览器进行访问。"
						});
		            	return;
		            } 
					
					dialog({
			  			type:'warn',
			  			title:'提示',
			  			content:"确认开始考试吗？",
			  			confirm:function(){
								$.ajax({
									type: "POST",
									url: "http://api.cp.gzkp.ktnw.cn/student/validation?unique="+id,
									data: {id:id},
									success: function(data) {
										$(".loading").hide();
										console.log(data);
										if(data.status){
											if("0"==data.message){
												dialog({
										  			type:'warn',
										  			title:'提示',
										  			content:"您好，您已测试"+data.times+"次,请问是否进入测试？",
										  			confirm:function(){
										  				window.open(data.page);
										  			}
												});
											}else if("1"==data.message){
												dialog({
										  			type:'error',
										  			title:'提示',
										  			content:"请求失败：原因：考试用户不存在。"
												});
											}else if("2"==data.message){
												dialog({
										  			type:'error',
										  			title:'提示',
										  			content:"请求失败：原因：考试次数用完。"
												});
											}else if("3"==data.message){
												dialog({
										  			type:'error',
										  			title:'提示',
										  			content:"请求失败：原因：其他。"
												});
											}
											that.getActivityBaomingxiangmuByXueshengId();
										}else{
											dialog({
									  			type:'error',
									  			title:'提示',
									  			content:"请求失败。"
											});
										}
									}
								});
			  			}
					});
				},
				deleteXiangmu(id){
					let that = this;
					dialog({
			  			type:'warn',
			  			title:'提示',
			  			content:"确认删除报名信息吗？",
			  			confirm:function(){
								$.ajax({
									type: "POST",
									url: ctx + "/deleteXiangmu.do",
									data: {id:id},
									success: function(data) {
										$(".loading").hide();
										if(data.state){
											dialog({
									  			type:'info',
									  			title:'提示',
									  			content:data.msg
											});
											that.getActivityBaomingxiangmuByXueshengId();
										}else{
											dialog({
									  			type:'error',
									  			title:'提示',
									  			content:data.msg
											});
										}
									}
								});
			  			}
					});
				},
				showPage(){
					let that =this;
					$(".m-page-item").html("");
					//整理页号
					if(that.pageCount == 0){
						$(".m-page-item").append('<div class="m-page-disable"><span>&gt;</span></div>');
						$(".m-page-item").append('<div class="m-page-disable"><span>&lt;</span></div>');
					}else if(that.pageCount <= 5){
						if(that.pageNum == that.pageCount){
							$(".m-page-item").append('<div class="m-page-disable"><span>&gt;</span></div>');
						}else{
							$(".m-page-item").append('<div class="m-page-next"><span>&gt;</span></div>');
						}
						for(var i=that.pageCount; i>0; i--){
							if(i == that.pageNum){
								$(".m-page-item").append('<div class="m-page-num m-page-active"><span>'+i+'</span></div>');
							}else{
								$(".m-page-item").append('<div class="m-page-num"><span>'+i+'</span></div>');
							}
						}
						if(that.pageNum == 1){
							$(".m-page-item").append('<div class="m-page-disable"><span>&lt;</span></div>');
						}else{
							$(".m-page-item").append('<div class="m-page-pre"><span>&lt;</span></div>');
						}
					}else{
						if(that.pageNum == that.pageCount){
							$(".m-page-item").append('<div class="m-page-disable"><span>&gt;</span></div>');
							$(".m-page-item").append('<div class="m-page-num m-page-active"><span>'+pageCount+'</span></div>');
						}else{
							$(".m-page-item").append('<div class="m-page-next"><span>&gt;</span></div>');
							$(".m-page-item").append('<div class="m-page-num"><span>'+pageCount+'</span></div>');
						}
						if(that.pageCount - that.pageNum >= 4){
							$(".m-page-item").append('<div><span>···</span></div>');
						}
						for(var i=that.pageNum+2; i>=that.pageNum-2; i--){
							if(i <= pageCount - 1 && i > 1){
								if(i == pageNum){
									$(".m-page-item").append('<div class="m-page-num m-page-active"><span>'+i+'</span></div>');
								}else{
									$(".m-page-item").append('<div class="m-page-num"><span>'+i+'</span></div>');
								}
							}
						}
						if(that.pageNum > 4){
							$(".m-page-item").append('<div><span>···</span></div>');
						}
						if(that.pageNum == 1){
							$(".m-page-item").append('<div class="m-page-num m-page-active"><span>1</span></div>');
							$(".m-page-item").append('<div class="m-page-disable"><span>&lt;</span></div>');
						}else{
							$(".m-page-item").append('<div class="m-page-num"><span>1</span></div>');
							$(".m-page-item").append('<div class="m-page-pre"><span>&lt;</span></div>');
						}
					}
				}
			},
		
			mounted(){
				let that = this;
				//分页点击
				$(document).on("click",".m-page-num",function(){
					if(!$(this).hasClass("m-page-active")){
						that.pageNum = parseInt($(this).find("span").html());
						that.showPage(that.pageNum,that.pageCount);
						that.getActivityBaomingxiangmuByXueshengId();
					}
				});
				
				//上一页
				$(document).on("click",".m-page-pre",function(){
					that.pageNum = parseInt($(".m-page-active").find("span").html()) - 1;
					that.showPage(that.pageNum,that.pageCount);
					that.getActivityBaomingxiangmuByXueshengId();
				});
				//下一页
				$(document).on("click",".m-page-next",function(){
					that.pageNum = parseInt($(".m-page-active").find("span").html()) + 1;
					that.showPage(that.pageNum,that.pageCount);
					that.getActivityBaomingxiangmuByXueshengId();
				});
				//跳至第几页
				$("#m-page-input").keypress(function (e) {
			        if (e.keyCode  == 13) {
			        	//判断字符串是否为数字 //判断正整数 /^[1-9]+[0-9]*]*$/
			        	var re = /^[0-9]+.?[0-9]*$/; 
			        	var num = $('#m-page-input').val();
						if (re.test(num)) {
			               	var pageNumTemp =parseInt(num);
							if(pageNumTemp >= 1 && pageNumTemp <= pageCount){
								that.pageNum = pageNumTemp;
								that.showPage(that.pageNum,that.pageCount);
								that.getActivityBaomingxiangmuByXueshengId();
							}
						}
			        }
				});
				
			}
		}
		var Component = Vue.extend(Main);
		new Component().$mount('#m-container');
});


